package org.sibframework.sda.monitor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SdaMonitor {
	
	private Date start = null;

	private long startedTrans = 0;
	
	private long finishedTrans = 0;
	
	private long createdConnection = 0;
	
	private long closedConnection = 0;

	private long requestConnection = 0;
	
	private long returnConnection = 0;
	
	private long createSDA = 0;
	
	private long closeSDA = 0;


	private long commit = 0;
	
	private long rollback = 0;
	

	private final Map<String, Long> transMap = new HashMap<String, Long>();
	private final Map<String, Long> sdaMap = new HashMap<String, Long>();
	
	private final static String UNKNOWN_SOURCE = "(no id)";
	
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public SdaMonitor() {
		super();
		this.start = new Date();
	}


	public synchronized void addStartedTrans(String id) {
		if (id == null || id.length() == 0) {
			id = UNKNOWN_SOURCE;
		}
		try {
			this.startedTrans++;

			Long count = transMap.get(id);
			if (count == null) {
				count = 0L;
			}
			count++;
			transMap.put(id, count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	public synchronized void addFinishedTrans(String id) {
		if (id == null || id.length() == 0) {
			id = UNKNOWN_SOURCE;
		}
		try {
			this.finishedTrans++;
			
			Long count = transMap.get(id);
			if (count == null) {
				count = 0L;
			}
			count--;
			transMap.put(id, count);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	public long getFinishedTrans() {
		return finishedTrans;
	}
	
	
	public synchronized long getStartedTrans() {
		return startedTrans;
	}
	
	public long getCreatedConnection() {
		return createdConnection;
	}
	public synchronized void addCreatedConnection() {
		this.createdConnection++;
	}
	
	
	public long getClosedConnection() {
		return closedConnection;
	}
	public synchronized void addClosedConnection() {
		this.closedConnection++;
	}
	
	
	public long getCreateSDA() {
		return createSDA;
	}
	///
	public synchronized void addCreateSDA(String id) {
		if (id == null || id.length() == 0) {
			id = UNKNOWN_SOURCE;
		}
		try {
			this.createSDA++;

			Long count = sdaMap.get(id);
			if (count == null) {
				count = 0L;
			}
			count++;
			sdaMap.put(id, count);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public synchronized void addCreateSDA() {
			this.createSDA++;

	}
	
	
	public long getCloseSDA() {
		return closeSDA;
	}

	public synchronized void addCloseSDA(String id) {
		if (id == null || id.length() == 0) {
			id = UNKNOWN_SOURCE;
		}
		try {
			this.closeSDA++;
			
			Long count = sdaMap.get(id);
			if (count == null) {
				count = 0L;
			}
			count--;
			sdaMap.put(id, count);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public synchronized void addCloseSDA() {
		this.closeSDA++;
	}

	public Map<String, Long> getTransMap() {
		return transMap;
	}


	public Map<String, Long> getSdaMap() {
		return sdaMap;
	}


	public long getCommit() {
		return commit;
	}


	public synchronized void addCommit() {
		this.commit++;
	}


	public long getRollback() {
		return rollback;
	}


	public synchronized void addRollback() {
		this.rollback++;
	}


	public String getStart() {
		return df.format(start);
	}

	/**
	 * 返回监控开始的小时
	 * @return
	 */
	public String getDuration() {
		float d = ((new Date().getTime() - start.getTime()) / (1000F * 60 * 60));
		d = (float) Math.ceil(d * 100) / 100F;
		return  d + "小时";
	}
	

	public long getRequestConnection() {
		return requestConnection;
	}


	public void addRequestConnection() {
		this.requestConnection++;
	}


	public long getReturnConnection() {
		return returnConnection;
	}


	public void addReturnConnection() {
		this.returnConnection++;
	}
	
	
}
